package com.sy.controller.message;

import com.sy.bean.message.Supplier;
import com.sy.bean.message.SupplierCategory;
import com.sy.bean.message.excel.GoodsExcel;
import com.sy.bean.message.excel.SupplierExcel;
import com.sy.bean.message.query.SupplierCategoryQuery;
import com.sy.bean.message.query.SupplierQuery;
import com.sy.common.bean.PageBean;
import com.sy.common.bean.ResultBean;
import com.sy.common.controller.BaseController;
import com.sy.service.message.SupplierCategoryService;
import com.sy.service.message.SupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Api(tags = "message-供应商管理")
@RequestMapping("/api")
public class SupplierController extends BaseController {
    @Autowired
    SupplierService supplierService;

    @Autowired
    SupplierCategoryService supplierCategoryService;

    @ApiOperation("供应商列表")
    @PostMapping("/supplier/list")
    public ResultBean querySupplier(@RequestBody SupplierQuery supplierQuery) {
        PageBean pageBean = supplierService.query(supplierQuery.toMap());
        return success("供应商列表", pageBean);
    }

    @ApiOperation("供应商新增")
    @PostMapping("/supplier/save")
    public ResultBean saveSupplier(@RequestBody Supplier supplier) {
        int rs = supplierService.save(supplier);
        return rs == 1 ? success("保存成功") : fail("保存失败");
    }

    @ApiOperation("删除供应商")
    @DeleteMapping("/supplier/del")
    public ResultBean delSupplier(int id) {
        int rs = supplierService.delete(id);
        return rs == 1 ? success("删除成功") : fail("删除失败");
    }

    @ApiOperation("编辑供应商")
    @PutMapping("/supplier/update")
    public ResultBean updateSupplier(@RequestBody Supplier supplier) {
        int rs = supplierService.update(supplier);
        return rs == 1 ? success("编辑成功") : fail("编辑失败");
    }

    @ApiOperation("供应商类别列表")
    @PostMapping("/category/list")
    public ResultBean queryCategory(@RequestBody SupplierCategoryQuery supplierCategoryQuery) {
        PageBean pageBean = supplierCategoryService.query(supplierCategoryQuery.toMap());
        return success("供应商类别列表", pageBean);
    }

    @ApiOperation("供应商类别新增")
    @PostMapping("/category/save")
    public ResultBean saveCategory(@RequestBody SupplierCategory supplierCategory) {
        int rs = supplierCategoryService.save(supplierCategory);
        return rs == 1 ? success("保存成功") : fail("保存失败");
    }

    @ApiOperation("删除供应商类别")
    @DeleteMapping("/category/del")
    public ResultBean delCategory(int id) {
        int rs = supplierCategoryService.delete(id);
        return rs == 1 ? success("删除成功") : fail("删除失败");
    }

    @ApiOperation("编辑供应商类别")
    @PutMapping("/category/update")
    public ResultBean updateCategory(@RequestBody SupplierCategory supplierCategory) {
        int rs = supplierCategoryService.update(supplierCategory);
        return rs == 1 ? success("编辑成功") : fail("编辑失败");
    }

    @PostMapping("/goods/exportSupplier")
    @ApiOperation("导出供应商列表")
    public ResultBean<SupplierExcel> exportSupplier(@RequestBody Map map) {
        String url = supplierService.exportSupplier(map);
        return success("导出供应商列表", url);
    }
}
